package com.atguigu.tingshu;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceAlbumApplication implements CommandLineRunner {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    AlbumInfoService albumInfoService;
    public static void main(String[] args) {
        SpringApplication.run(ServiceAlbumApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
//        System.out.println("album服务启动初始化："+redissonClient);
        //1、获取布隆过滤器
        String bloomFilterKey = RedisConstant.ALBUM_BLOOM_FILTER;
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(bloomFilterKey);
//        bloomFilter.tryInit(  , 0.03);
        //2、查询专辑id列表存到布隆过滤器中
        List<AlbumInfo> list = albumInfoService.list(Wrappers.lambdaQuery(AlbumInfo.class)
                .select(AlbumInfo::getId));
        //初始化布隆过滤器
        bloomFilter.tryInit(list.size() , 0.03);
        list.forEach(albumInfo -> {
            bloomFilter.add(albumInfo.getId());
        });
    }
}
